<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
      <el-button v-if="in_right_admin('UserLevel/edit_level')" @click="edit_level(row)" size="small">修改等级</el-button>
    </template>
  </list_url>
</template>
<script>
import {iframe, in_right_admin} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import {_config} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";
import {vue_template} from "@/js/dom_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');

    if(in_right_admin('UserLevel/add')) btns['添加'] = function(){ iframe('user_level/edit') }
    if(in_right_admin('UserLevel/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }

    let forms_config = {};
    let function_map = {};
    ajax({url:'/admin/user_level/function_config',cache:false,async:false},function (res){
      function_map = res.data.function_map;
      return false;
    })
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', type: 'hidden', order:true},
      level:{name:'level', label:'等级', width:'60px'},
      name:{name:'name', label:'等级名称', width:'120px'},
      direct_up:{name:'direct_up', label:'可直升', width:'55px',type:'radio_switch',config:'否|0,是|1'},
      can_down:{name:'can_down', label:'可降级', width:'55px',type:'radio_switch',config:'否|0,是|1'},
      condition_info:{name:'condition_info', label:'晋升条件'},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'140px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/user_level/'+(is_del?'recycle':'list'),
        title: "用户等级列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          s94Mjs.each(list, function(row){
          	let condition_arr = JSON.parse(row.condition);
          	row.condition_info = condition_arr.length ? '' : '无法自然晋升';
          	condition_arr.forEach(function (v,i){
                  let info = app.condition_info(v);
                  if (i>0 && info) info = (v.and==1 ? '【并且】' : '【或者】')+info;
                  row.condition_info += info;
              })
          })
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
      function_map: function_map,
    }
  },
  methods: {
    in_right_admin: in_right_admin,
    to_edit(row){
      let can_edit = in_right_admin('UserLevel/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      iframe('user_level/edit?'+s94Mjs.param_encode(param))
    },
    condition_info(condition){
      if (!this.function_map[condition.function]) return '';
      let fuc = this.function_map[condition.function];
      let data = fuc.info_param || [];
      fuc.param.forEach(function(k,i){
        data[k] = condition.param[i];
      })
      return vue_template(fuc.info, data);
    },
    edit_level(row){
      this.$prompt('输入新的等级序列，当前等级：'+row.level,'修改等级').then((data)=>{
        ajax({url:'/admin/user_level/edit_level?id='+row.id, data:{level:data.value}}, (res)=>{
          if (res.data>0) this.$refs['list_url'].list();
        })
      }).catch(()=>{})
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;


  }
}
</script>
